#include <sys/stat.h>
#include <sys/time.h>
#include <errno.h>
#include <time.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/times.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <string.h>

#define XMODEM_SOH 0x01
#define XMODEM_STX 0x02
#define XMODEM_EOT 0x04
#define XMODEM_ACK 0x06
#define XMODEM_NAK 0x15
#define XMODEM_CAN 0x18
#define XMODEM_CRC_CHR	'C'
#define XMODEM_CRC_SIZE 2		/* Crc_High Byte + Crc_Low Byte */
#define XMODEM_FRAME_ID_SIZE 2 		/* Frame_Id + 255-Frame_Id */
#define XMODEM_DATA_SIZE_SOH 128  	/* for Xmodem protocol */
#define XMODEM_DATA_SIZE_STX 1024 	/* for 1K xmodem protocol */
#define USE_1K_XMODEM 1  		/* 1 for use 1k_xmodem 0 for xmodem */
#define	TIMEOUT_USEC	0
#define	TIMEOUT_SEC		10

#if (USE_1K_XMODEM)
	#define XMODEM_DATA_SIZE 	XMODEM_DATA_SIZE_STX
	#define XMODEM_HEAD		XMODEM_STX
#else
	#define XMODEM_DATA_SIZE 	XMODEM_DATA_SIZE_SOH
	#define XMODEM_HEAD 		XMODEM_SOH
#endif

unsigned short get_crc16 ( char *ptr, unsigned short count )
{
	unsigned short crc, i;

	crc = 0;
	while(count--)
	{
		crc = crc ^ (int) *ptr++ << 8;

		for(i = 0; i < 8; i++)
		{
			if(crc & 0x8000)
				crc = crc << 1 ^ 0x1021;
			else
				crc = crc << 1;
		}
	}

	return (crc & 0xFFFF);
}

void char_to_hex(char * dest, unsigned char * src)
{
    int i;
    unsigned char hex = 0;
    int index = 0;
    
    for(i=0;i<strlen((char *)src);i=i+2)
    {
        if((src[i]>='0')&&(src[i]<='9'))
        {
            hex = (src[i]-'0')<<4;
        }
        else if((src[i]>='A')&&(src[i]<='F'))
        {
            hex = (src[i]-'A'+10)<<4;
        }
        
        if((src[i+1]>='0')&&(src[i+1]<='9'))
        {
            hex |= (src[i+1]-'0');
        }
        else if((src[i+1]>='A')&&(src[i+1]<='F'))
        {
            hex |= (src[i+1]-'A'+10);
        }
        dest[index++] = hex;
    }
 }

char * hex_to_char(char * dst, unsigned char * src, int len)
{
    int i = 0;

    for(i = 0; i < len; i++)
    {
        sprintf((char *)dst,"%02X",src[i]);
        dst+=2;
    }

    return dst;
}

int main()
{
	FILE * fa, *fc, *fwa, *fwc;
	unsigned char packet_data[1024] = {0};
	char packet_char[2048] = {0};
	char frame_data[1029] = {0};
	char complete = 0;
	int retry_num,pack_counter,read_number,write_number,i;
	unsigned short crc_value;
	unsigned char ack_id = 'C';
	char * pdata;
	char temp[10] = {0};

	fa = fc = fwa = fwc = NULL;

	fa=fopen("bootloader_r3.0.0_build6773_uartboot_460800.pkg","rb"); 
	fc=fopen("UM220-IV_NV_M0-R3.2.0.18Build6658_mfg.pkg","rb");

	fwa = fopen("bootloader-hex.cpp", "w");
	fwc = fopen("firmware-hex.cpp", "w");	
	
	pdata = "unsigned char bootloader[][1024] = {";
	fwrite(pdata, sizeof(char), strlen(pdata), fwa);

	i = 0;
	while (!complete) {

		read_number = fread(packet_data, sizeof(char), XMODEM_DATA_SIZE, fa);

		if(read_number > 0)//read_number为返回的读取实际字节数
		{
			printf("read_number[%d]:%d\n", i, read_number);
			if(read_number < XMODEM_DATA_SIZE_STX)
			{
				printf("Start filling the last frame!\n");
				for(; read_number < XMODEM_DATA_SIZE; read_number++)
					packet_data[read_number] = 0x1A;  // 不足128字节用0x1A填充
			}

			pdata = "\n    ";
			fwrite(pdata, sizeof(char), strlen(pdata), fwa);
			for (i = 0 ; i < read_number; i++) {
				sprintf(temp, "0x%02x, ", packet_data[i]);
				fwrite(temp, 1, strlen(temp), fwa);	
				memset(temp, 0, sizeof(temp));
			}

			fflush(stdout);
		}

		else  // 文件发送完成
		{
			pdata = "\n};\n\n";
			fwrite(pdata, sizeof(char), strlen(pdata), fwa);
			complete = 1;
			fclose(fa);
			fa = NULL;
			fclose(fwa);
			fwa = NULL;
		}
	}

	pdata = "unsigned char firmware[][1024] = {";
	fwrite(pdata, sizeof(char), strlen(pdata), fwc);
	
	#if 1
	i = 0;
	complete = 0;
	while (!complete) {

		read_number = fread(packet_data, sizeof(char), XMODEM_DATA_SIZE, fc);

		if(read_number > 0)//read_number为返回的读取实际字节数
		{
			printf("read_number[%d]:%d\n", i, read_number);
			if(read_number < XMODEM_DATA_SIZE_STX)
			{
				printf("Start filling the last frame!\n");
				for(; read_number < XMODEM_DATA_SIZE; read_number++)
					packet_data[read_number] = 0x1A;  // 不足128字节用0x1A填充
			}

			pdata = "\n    ";
			fwrite(pdata, sizeof(char), strlen(pdata), fwc);
			for (i = 0 ; i < read_number; i++) {
				sprintf(temp, "0x%02x, ", packet_data[i]);
				fwrite(temp, 1, strlen(temp), fwc);	
				memset(temp, 0, sizeof(temp));
			}

			fflush(stdout);
		}

		else  // 文件发送完成
		{
			pdata = "\n};\n\n";
			fwrite(pdata, sizeof(char), strlen(pdata), fwc);
			complete = 1;
			fclose(fc);
			fc = NULL;
			fclose(fwc);
			fwc = NULL;
		}
	}
	#endif 
}



/*











*/
